﻿using Model.Core;
using Models;
using SIE.SCADA.Common.Log;
using SIE.SCADA.ZemtDemo.Common;
using System;
using System.Collections.Generic;
using System.Text;

namespace SIE.SCADA.StationDemo.Common
{
    public class ApiMeth
    {
        private string _serverIp;
        public ApiMeth()
        {
            _serverIp = Env.StationConfig.ServerIP;
        }
        #region 调用的API方法
        /// <summary>
        /// 请求工单
        /// </summary>
        /// <param name="barcodeMsg"></param>
        /// <returns></returns>
        public OrderMsg SendScanBardcode(string barcodeMsg)
        {
            OrderMsg orderMsg = new OrderMsg();
            var arrlist = new List<object>();
            arrlist.Add(Env.StationConfig.StationId);
            arrlist.Add(barcodeMsg);
            try
            {
                var res = ApiHelper.Post<OrderMsg>("GetOrderMsg", arrlist.ToArray(), Env.StationConfig.ServerIP);
                if (res.IsSuccess)
                {
                    if (res.Data != null)
                    {
                        var dataRes = res.Data;
                        return dataRes;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
            }
            return orderMsg;
        }

        /// <summary>
        /// 获取当前子工序信息
        /// </summary>
        /// <returns></returns>
        public OrderMsg SendRequestCurrOrder()
        {
            OrderMsg orderMsg = new OrderMsg();
            var arrlist = new List<object>();
            arrlist.Add(Env.StationConfig.StationId);
            try
            {
                var res = ApiHelper.Post<OrderMsg>("GetCurSubProcess", arrlist.ToArray(), Env.StationConfig.ServerIP);
                if (res.IsSuccess)
                {
                    if (res.Data != null)
                    {
                        var dataRes = res.Data;
                        return dataRes;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
            }
            return orderMsg;
        }

        /// <summary>
        /// 请求过站
        /// </summary>
        /// <param name="_Scada_Jobsteprecord"></param>
        /// <returns></returns>
        public bool SendUpdateRecord(List<scada_jobstep_record> jobstep_Record)
        {
            try
            {
                var arrlist = new List<object>();
                arrlist.Add(Env.StationConfig.StationId);
                arrlist.Add(jobstep_Record);
                var res = ApiHelper.Post<bool>("UpdateWorkState", arrlist.ToArray(), _serverIp);
                if (res.IsSuccess)
                {
                    var dataRes = res.Data;
                    return dataRes;
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message);
            }
            return false;
        }

        /// <summary>
        /// KJF 20210323 修改
        /// 缓存恢复的接口
        /// </summary>
        /// <param name="jobstep_Record"></param>
        /// <returns></returns>
        public bool SendRecoveryRecord<T>(List<T> items,string RecoveryFunctionName)
        {
            try
            {
                var arrlist = new List<object>();
                arrlist.Add(Env.StationConfig.StationId);
                arrlist.Add(items);
                var res = ApiHelper.Post<bool>(RecoveryFunctionName, arrlist.ToArray(), _serverIp);
                if (res.IsSuccess)
                {
                    var dataRes = res.Data;
                    return dataRes;
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message);
            }
            return false;
        }

        /// <summary>
        /// 请求重打的步
        /// </summary>
        /// <returns></returns>
        public OrderMsg GetRetryStep()
        {
            OrderMsg orderMsg = new OrderMsg();
            try
            {
                var arrlist = new List<object>();
                arrlist.Add(Env.StationConfig.StationId);
                var res = ApiHelper.Post<OrderMsg>("GetRetryRecordStep", arrlist.ToArray(), _serverIp);
                if (res.IsSuccess)
                {
                    return res.Data;
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message);
            }
            return orderMsg;
        }

        /// <summary>
        /// KJF 20210324 （作废）
        /// 请求重打的工序
        /// </summary>
        /// <returns></returns>
        public bool SendRetryProcess()
        {
            bool resutl = false;
            try
            {
                var arrlist = new List<object>();
                arrlist.Add(Env.StationConfig.StationId);
                var res = ApiHelper.Post<bool>("GetRetryRecordProcess", arrlist.ToArray(), _serverIp);
                if (res.IsSuccess)
                {
                    resutl = res.Data;
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message);
            }
            return resutl;
        }

        /// <summary>
        /// 发送客户信息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool SendClientMsg(string msg)
        {
            try
            {
                var arrlist = new List<object>();
                arrlist.Add(Env.StationConfig.StationId);
                arrlist.Add(msg);
                var res = ApiHelper.Post<bool>("RecvMsg", arrlist.ToArray(), _serverIp);
                if (res.IsSuccess)
                {
                    var dataRes = res.Data;
                    return dataRes;
                }
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message);
            }
            return false;
        }

        public bool SendTorqParam(TorqueParmas torqueParmas)
        {
            try
            {
                var arrlist = new List<object>();
                arrlist.Add(Env.StationConfig.StationId);
                arrlist.Add(torqueParmas);
                var res = ApiHelper.Post<bool>("TroqueParmaRecord", arrlist.ToArray(), _serverIp);
                if (res.IsSuccess)
                {
                    var dataRes = res.Data;
                    return dataRes;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message);
                Logger.Error(ex.StackTrace);
            }
            return false;
        }
        #endregion 
    }
}
